package com.example.demo.handler;

import com.example.demo.util.ResultModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * Description:全局变量捕获
 * @author huangweicheng
 * @date 2019/10/22   
*/
@ControllerAdvice
public class GlobalExceptionHandler
{
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResultModel> exceptionHandler(Exception e)
    {
        e.printStackTrace();
        ResultModel resultModel = new ResultModel(2,"系统出小差了，让网站管理员来处理吧 ಥ_ಥ");
        return new ResponseEntity<>(resultModel, HttpStatus.OK);
    }

    @ResponseBody
    @ExceptionHandler(ImageCodeException.class)
    public ResponseEntity<ResultModel> exceptionHandler(ImageCodeException e)
    {
        e.printStackTrace();
        ResultModel resultModel = new ResultModel(2,e.getMessage());
        return new ResponseEntity<>(resultModel,HttpStatus.OK);
    }
}